package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoardService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "文章类接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IBoardService boardService;

    @Resource
    private IArticleService articleService;

    @ApiOperation("发布帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("版块id") @RequestParam("boardId") @NotNull Long boardId,
                            @ApiParam("文章标题") @RequestParam("title") @NotNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NotNull String content){
        User user=AppConfig.getUser(request);
        if(user==null||user.getDeleteState()!=0||user.getState()!=0){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        Board board=boardService.selectById(boardId);
        if(board==null||board.getDeleteState()!=0||board.getState()!=0){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_BOARD_BANNED);
        }

        Article article=new Article();
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        article.setTitle(title);
        article.setContent(content);

        articleService.create(article);

        return AppResult.success();
    }

    @ApiOperation("获取版块列表")
    @GetMapping("/getListByBoardId")
    public AppResult<List<Article>> getListByBoardId(@ApiParam("版块id") @RequestParam(value = "boardId",required = false) Long boardId){
        List<Article> list=null;
        if(boardId==null){
            list=articleService.selectAll();
        }else{
            list=articleService.selectByBoardId(boardId);
        }
        if(list==null){
            list=new ArrayList<>();
        }
        return AppResult.success(list);
    }

    @ApiOperation("根据id获取帖子详情")
    @GetMapping("/getDetail")
    public AppResult getDetail(HttpServletRequest request,
                               @ApiParam("文章id") @RequestParam("id") @NotNull Long id){
        Article article=articleService.selectDetailById(id);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        User user=AppConfig.getUser(request);
        if(user.getId().equals(article.getUserId())){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    @ApiOperation("修改帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("文章id") @RequestParam("id") @NotNull Long id,
                            @ApiParam("文章标题") @RequestParam("title") @NotNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NotNull String content){
        User user=AppConfig.getUser(request);
        if(user.getDeleteState()==1||user.getState()==1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        Article article=articleService.selectById(id);
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        if(!article.getUserId().equals(user.getId())){
            log.warn(ResultCode.FAILED_UNAUTHORIZED.toString());
            return AppResult.fail(ResultCode.FAILED_UNAUTHORIZED);
        }

        articleService.modify(id,title,content);
        return AppResult.success();
    }

    @ApiOperation("点赞功能")
    @PostMapping("/thumbsUpById")
    public AppResult thumbsUpById(HttpServletRequest request,
                                  @ApiParam("文章id") @RequestParam("id") @NotNull Long id){
        User user=AppConfig.getUser(request);
        if(user.getState()==1||user.getDeleteState()==1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    @ApiOperation("根据id删除帖子")
    @PostMapping("/deleteById")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("文章id") @RequestParam("id") @NotNull Long id){
        User user=AppConfig.getUser(request);
        if(user.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        Article article=articleService.selectById(id);
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(!user.getId().equals(article.getUserId())){
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.fail(ResultCode.FAILED_FORBIDDEN);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }

    @ApiOperation("获取用户个人文章列表")
    @GetMapping("/getAllByUserId")
    public AppResult getAllByUserId(HttpServletRequest request,
                                    @ApiParam("用户id") @RequestParam(value = "userId",required = false) Long userId){
        if(userId==null){
            User user=AppConfig.getUser(request);
            userId=user.getId();
        }
        List<Article> articles=articleService.selectAllByUserId(userId);
        return AppResult.success(articles);
    }
}
